//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using CommunityServer.Components;

namespace CommunityServer.Blogs.Components
{
	/// <summary>
	/// Summary description for BlogPermission.
	/// </summary>
	public class WeblogPermission : PermissionBase
	{
		public WeblogPermission()
		{
		}

        #region Public Properties
        /// <summary>
        /// Can the current User View the Blog
        /// </summary>
        public virtual bool View 
        {
            get{ return GetBit( Permission.View ); }
        }

        /// <summary>
        /// Can the current User edit the Blogs Content
        /// </summary>
        public virtual bool Post 
        {
            get{ return GetBit( Permission.Post ); }
        }

        /// <summary>
        /// Can the current User post comments
        /// </summary>
        public virtual bool Reply 
        {
            get{ return GetBit( Permission.Reply ); }
        }

        /// <summary>
        /// Can the current User add attachments to posts
        /// </summary>
        public virtual bool LocalAttachment 
        {
            get{ return GetBit( Permission.LocalAttachment ); }
        }

        /// <summary>
        /// Can the current User add attachments to posts
        /// </summary>
        public virtual bool RemoteAttachment 
        {
            get{ return GetBit( Permission.RemoteAttachment ); }
        }

        /// <summary>
        /// Can the current User administer their own blog settings
        /// </summary>
        public virtual bool Administer 
        {
            get{ return GetBit( Permission.Administer ); }
        }

        /// <summary>
        /// Can the current User mark posts as sticky
        /// </summary>
        public virtual bool Sticky 
        {
            get{ return GetBit( Permission.Sticky ); }
        }

        /// <summary>
        /// Can the current User/Role delete posts
        /// </summary>
        public virtual bool Delete 
        {
            get{ return GetBit( Permission.Delete ); }
        }

		/// <summary>
		/// Can the current User/Role use Ink when creating/editing posts
		/// </summary>
		public virtual bool Ink 
		{
			get{ return GetBit( Permission.Ink ); }
		}

		/// <summary>
		/// Can the current User/Role add a video when creating/editing posts
		/// </summary>
		public virtual bool Video 
		{
			get{ return GetBit( Permission.Video ); }
		}

        #endregion


        #region Validate

        public static bool Validate(Section section, Permission permission, User user, Post p)
        {

			if(user.IsBlogAdministrator)
				return true;

            if(section == null || section.PermissionSet == null || user == null )
                return false;

            bool bReturn = true;
            WeblogPermission fpFinal = section.ResolvePermission(user) as WeblogPermission;

            if((permission == (permission | Permission.Administer)) && bReturn )
                bReturn &= fpFinal.Administer;

            if((permission == (permission | Permission.Delete)) && bReturn ) 
                bReturn &= fpFinal.Delete;

            if((permission == (permission | Permission.Post)) && bReturn )
                bReturn &= fpFinal.Post;

            if((permission == (permission | Permission.Reply)) && bReturn )
                bReturn &= fpFinal.Reply;

            if((permission == (permission | Permission.Sticky)) && bReturn )
                bReturn &= fpFinal.Sticky;

            if((permission == (permission | Permission.View)) && bReturn )
                bReturn &= fpFinal.View;
            
            if((permission == (permission | Permission.RemoteAttachment)) && bReturn )
                bReturn &= fpFinal.RemoteAttachment;

            if((permission == (permission | Permission.LocalAttachment)) && bReturn )
                bReturn &= fpFinal.LocalAttachment;

			if((permission == (permission | Permission.Ink)) && bReturn )
				bReturn &= fpFinal.Ink;

			if((permission == (permission | Permission.Video)) && bReturn )
				bReturn &= fpFinal.Video;
			
			return bReturn;
        }

        #endregion


        #region AccessCheck

        public static void AccessCheck(Section section, Permission permission, User user, Post post)
        {
            if (user.IsBlogAdministrator)
                return;

                        CSContext csContext = CSContext.Current;

            WeblogPermission fp = section.ResolvePermission( user ) as WeblogPermission;
            long lValue = (long)permission;
            switch (lValue) 
            {
//                 case (long)Permission.Attachment:
//                    // TODO may need to add configuration value to allow anonymous attachments
//                    if (!csContext.Context.Request.IsAuthenticated)
//                        throw new CSException(CSExceptionType.AccessDenied);
//
//                    if(!fp.Attachment)
//                        throw new CSException(CSExceptionType.PostAttachmentsNotAllowed);
//                    break;
                case (long)Permission.Post:
                    if (!fp.Post)
                    {   
                        RedirectOrExcpetion(CSExceptionType.PostAccessDenied, section.Name);
                    }
                    break;

                case (long)Permission.Reply:
                    if ((csContext.User.IsAnonymous) && ((!section.EnableAnonymousPosting) || (!csContext.SiteSettings.EnableAnonymousUserPosting)))
                            RedirectOrExcpetion(CSExceptionType.PostReplyAccessDenied);
                    if (!fp.Reply) 
                        RedirectOrExcpetion(CSExceptionType.PostReplyAccessDenied);
                    break;

                case (long)Permission.View:
                    if(!fp.View)
                        RedirectOrExcpetion(CSExceptionType.SectionNotFound, "Weblog does not exists or View Permission is denied on " + section.Name);
                    break;

                case (long)Permission.LocalAttachment:
                    if(!fp.LocalAttachment)
                        throw new CSException(CSExceptionType.PostAttachmentsNotAllowed,"You do not have permission to add linked attachments");
                    break;

                case (long)Permission.RemoteAttachment:
                    if(!fp.RemoteAttachment)
                        throw new CSException(CSExceptionType.PostAttachmentsNotAllowed,"You do not have permission to upload attachments");
                    break;

				case (long)Permission.Ink:
					if(!fp.Ink)
						RedirectOrExcpetion(CSExceptionType.UnknownError,"User does not have permission to use ink");
					break;

				case (long)Permission.Video:
					if(!fp.Video)
						RedirectOrExcpetion(CSExceptionType.UnknownError,"User does not have permission to post a video");
					break;

				default:
                    RedirectOrExcpetion(CSExceptionType.AccessDenied);
                    break;
            }
        }

        #endregion


	}
}
